/**
 * problem_003.c
 * Copyright (C) 2011-03-17 - xrose
 * What is the largest prime factor of the number 600851475143 ?
 * result: 6857
 */
#include <stdio.h>
#include <math.h>

int main (int argc, char *argv[])
{
    long long int numb = 600851475143;
    long sqrtnumb = floor(sqrt(numb));
    long i, maxfactor;

    for (i=3; i <= sqrtnumb; i = i + 2)
    {
        if(isPrime(i))
        {
            if(numb % i == 0)
            {
                maxfactor = i;
            }
        }
    }
    printf("The largest prime factor of the number 600851475143 is: %ld", maxfactor);
    return 0;
}
/*is a prime?*/
int isPrime(long numb)
{
    int sqrtnumb = floor(sqrt(numb));
    int i;
    if(numb%2 == 0) return 0;
    for(i = 3; i<=sqrtnumb; i=i+2)
    {
        if(numb%i == 0)
        {
            return 0;
        }
    }
    return 1;
}
